package model

import (
	"fmt"
	"goweb/ext/settings"
	"reflect"

	"github.com/globalsign/mgo/bson"
)

type T struct {
	// `bson:"_id"`
	Id          string `bson:"_id"`
	Name        string `bson:"Name"`
	UserId      string `bson:"UserId"`
	FollowCount int64  `bson:"FollowCount"`
	Fid         int64  `bson:"Fid"`
	Follower    string `bson:"Follower"`
}

var db = "weibo"
var doc = "rules"

func Test(t T) []T {
	c := conf.MgoConn.DB(doc).C(db)

	m := map[string]interface{}{
		"FollowCount": 194,
	}
	fmt.Println(t.FollowCount)
	outs, _ := bson.Marshal(m)
	count, _ := c.Find(outs).Count()
	var res []T
	c.Find(bson.M{"FollowCount": 194}).All(&res)
	fmt.Println(res)
	fmt.Println(count)
	return res
}

func Struct2Map(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)

	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		data[t.Field(i).Name] = v.Field(i).Interface()
	}
	return data
}
